<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\PackageSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;

use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Store;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\DbConnection\Model\Model;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/package")]
class PackageController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        $package_name = $this->request->input('name', '');
        $type = $this->request->input('type', '');
        if ($package_name != '') {
            $where[] = ['name', 'like', '%' . $package_name . '%'];
        }
        if ($type != '') {
            $where[] = ['type', '=', $type];
        }
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            $where[] = ['admin_id', '=', $admin_user['admin_user_id']];
        }

        $lists = Package::where($where)
            ->orderBy('package_id', 'DESC')
            ->paginate();

        return compact('lists', 'admin_user');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new PackageSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $package_id = $request_param->getPackageId();
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        $store = Store::where('admin_id', $admin_user['admin_user_id'])->select('store_id')
            ->first() ?: [];
        $res = Package::createPackage($package_id, [
            'name' => $request_param->getName()
            , 'price' => $request_param->getPrice()
            , 'time' => $request_param->getTime()
            , 'type' => $request_param->getType()
            , 'status' => $request_param->getStatus()
            , 'admin_id' => $admin_user['admin_user_id']
            , 'store_id' => $store['store_id']

        ]);

        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $package_id = (int)$this->request->input('package_id', 0);

        return ['title' => $package_id > 0 ? '编辑套餐' : '新增套餐'];
    }


    #[Api]
    #[GetMapping("edit/{package_id}")]
    function editInfo(int $package_id)
    {
        $package = Package::where('package_id', $package_id)->select('*')
            ->first() ?: [];
        return compact('package');
    }


}
